<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 15:22:10
 * @LastEditTime: 2023-09-02 12:21:26
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 * 函数 定义protected，就先访问 APP\VALIDATE\  下同目录  同类 同函数
 * 该函数 应返回 [RULE,MSG] 验证结构数据  用于 验证
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Laravel\Lumen\Routing\Controller as BaseController;

use Exception;

class Controller extends BaseController
    {
    //
    public function __construct()
        {
        if (method_exists($this, 'boot')) {
            App::call([$this, 'boot']);
            }
        }
    public $service;
    public function info(Request $request)
        {
        return $this->service->info($request);
        }
    protected function enable()
        {
        return $this->service->enable();
        }

    protected function disable()
        {
        return $this->service->disable();
        }
    public function list()
        {
        return $this->service->list();
        }

    protected function edit(Request $request)
        {
        return $this->service->edit($request);
        }
    protected function add(Request $request)
        {
        return $this->service->edit($request);
        }

    /**
     * 控制器没有定义接口，callController函数会调用callend
     * @return mixed
     * @author sunkaiyuan
     */
    public function callend()
        {
        return $this->{getRouter('action')}();
        }
    public function __call($name, $arguments)
        {
        try {
            //验证参数
            $this->callValidateClass($name);
            if (method_exists($this, $name)) {
                return $this->$name(...$arguments);
                }
            else if (method_exists($this->service, $name)) {
                //没有定义方法，服务层有，直接调用服务层
                return App::call([$this->service, $name]);
                }
            }
        catch ( Exception $e ) {
            fail($e->getMessage());
            }
        fail("网页不存在", 404);
        }


    //执行验证方法
    private function callValidateClass($functionName)
        {
        $validateClass = getValidate();

        if (class_exists($validateClass) && method_exists($validateClass, $functionName)) {
            $validApp          = App::make($validateClass);
            [$rules, $message] = App::call([$validApp, $functionName]);
            $this->validate(request(), $rules, $message);
            }
        }

    protected function throwValidationException(Request $request, $validator)
        {
        $errmsg = $this->formatValidationErrors($validator);
        fail($this->getFirstValue($errmsg), 417, $errmsg);
        }
    /**
     * Summary of getFirstValue
     * @param mixed $array
     * @return mixed
     */
    protected function getFirstValue($array = [])
        {
        foreach ($array as $v) {
            if (is_array($v))
                return $this->getFirstValue($v);
            else
                return $v;
            }
        }

    }